package me.pplay.playtalk.view;

import me.pplay.playtalk.R;
import me.pplay.playtalk.adapter.GridMenuAdapter;
import me.pplay.playtalk.util.AsyncImageLoader;
import me.pplay.playtalk.util.AsyncImageLoader.ImageCallback;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;

public class MenuDialog {
	private Context mContext;
	public GridView gridView;
	private int[] menu_drawableIds; //= {R.drawable.icon_1, R.drawable.icon_2, R.drawable.icon_3, R.drawable.icon_4, R.drawable.icon_5, R.drawable.icon_6};
	private String[] menu_titles;
	private AlertDialog dialog;
	private String dialogTitle, dialogIconUrl, drawableUrl;
	private int dialogIconId, drawableId;
	private boolean bool;
	private AsyncImageLoader imageLoader;
	
	//bool true
	public MenuDialog(Context context, int[] menu_drawableIds, String[] menu_titles, String dialogTitle, int dialogIconId, String dialogIconUrl, boolean bool, AsyncImageLoader imageLoader){
		this.mContext			= context;
		this.menu_drawableIds	= menu_drawableIds;
		this.menu_titles		= menu_titles;
		this.dialogTitle		= dialogTitle;
		this.dialogIconId		= dialogIconId;
		this.dialogIconUrl		= dialogIconUrl;
		this.imageLoader		= imageLoader;
		this.bool				= bool;
		dialog();
	}

	//bool false
	public MenuDialog(Context context, int drawableId, String drawableUrl, String[] menu_titles, String dialogTitle, int dialogIconId, String dialogIconUrl, boolean bool, AsyncImageLoader imageLoader){
		this.mContext			= context;
		this.drawableId			= drawableId;
		this.drawableUrl		= drawableUrl;
		this.menu_titles		= menu_titles;
		this.dialogTitle		= dialogTitle;
		this.dialogIconId		= dialogIconId;
		this.dialogIconUrl		= dialogIconUrl;
		this.imageLoader		= imageLoader;
		this.bool				= bool;
		dialog();
	}
	
	
	private void dialog(){
		dialog			= new AlertDialog.Builder(mContext).create();
		dialog.setTitle(dialogTitle);
		if(dialogIconUrl != null){//异步获取头像
			setImageIcon(dialogIconUrl);
		}else{
			dialog.setIcon(dialogIconId);
		}
			
		View menuview 	= LayoutInflater.from(mContext).inflate(R.layout.layout_gridview_menu, null);
		dialog.setView(menuview);
		gridView		= (GridView)menuview.findViewById(R.id.gridview);
		if(bool)
			gridView.setAdapter(new GridMenuAdapter(mContext, menu_drawableIds, menu_titles, bool));
		else
			gridView.setAdapter(new GridMenuAdapter(mContext, drawableId,drawableUrl, menu_titles, bool, imageLoader));
	}
	
	public void showMenu(){
		dialog.show();
	}
	
	public boolean isShowing(){
		return dialog.isShowing();
	}
	
	public void cancelMenu(){
		dialog.dismiss();
	}
	
	
	private void setImageIcon(final String imageUrl){
		Bitmap bitmap = imageLoader.loadBitmap(imageUrl, new ImageCallback() {
			
			@Override
			public void imageLoaded(Bitmap imageBitmap, String imageUrl) {
				if(imageBitmap != null)
					dialog.setIcon(new BitmapDrawable(imageBitmap));
			}
		});
		if(bitmap != null)
			dialog.setIcon(new BitmapDrawable(bitmap));
	}
}
